/**
 * 
 */
package com.ruleengine.task.handler.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruleengine.parser.ITaskContants;
import com.ruleengine.task.ITask;
import com.ruleengine.task.handler.IExceptionHandler;
import com.ruleengine.task.handler.IHandler;

/**
 * @author wuxiao
 *
 * Created on Apr 15, 2012
 */
public class DefaultExceptionHandler implements IExceptionHandler {
    private List<IHandler> handlers = new ArrayList<IHandler>();
    private ITask finallyTask;
    
    /* (non-Javadoc)
     * @see com.wx.workflow.task.handler.IExceptionHandler#getFinallyTask()
     */
    public ITask getFinally() {
        // TODO Auto-generated method stub
        return finallyTask;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.task.handler.IExceptionHandler#getHandlers()
     */
    public List<IHandler> getHandlers() {
        // TODO Auto-generated method stub
        return handlers;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.task.handler.IExceptionHandler#setFinallyTask(com.wx.workflow.task.ITask)
     */
    public void setFinally(ITask task) {
        // TODO Auto-generated method stub
        this.finallyTask = task;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.task.handler.IExceptionHandler#setHandlers(java.util.List)
     */
    public void setHandlers(List<IHandler> handlers) {
        // TODO Auto-generated method stub
        this.handlers = handlers;
    }

    public void addHandler(IHandler handler) {
        // TODO Auto-generated method stub
        this.handlers.add(handler);
    }
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        StringBuffer tag = new StringBuffer();
        tag.append("<"+ITaskContants.ELE_TAG_EXCEPTIONHANDLERS+">");
        for(IHandler handler : handlers){
            tag.append(handler);
        }
        if(finallyTask !=null){
            tag.append("<"+ITaskContants.ELE_TAG_FINALLY+">");
            tag.append(finallyTask);
            tag.append("</"+ITaskContants.ELE_TAG_FINALLY+">");
        }
        tag.append("</"+ITaskContants.ELE_TAG_EXCEPTIONHANDLERS+">");
        return tag.toString();
    }
}
